/**
 * @Title: detail.js
 * @Description: detail
 * @author: Fanchao
 * @date: 2020/8/28 10:25 下午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import { answerEvaluateExamItem, getEvaluateExamItemDetail } from 'api/index';


export default {
    namespace: 'myTaskDetail',
    state: {
        evaluateExamItemId: null,
        taskStartTime: null,
        taskEndTime: null,
        evaluateObjectModel: null,
        evaluatePlanSnapshotsModel: null,
        questionSnapshotsModelList: null,
        initExamItemAnswerDetailMsgList: [],
        showBtn: true,
        evaluateTaskType: '',
        expertCheckCourseInfo: {},
    },
    reducers: {
        setEvaluateExamItemId(state, { payload: evaluateExamItemId }) {
            return { ...state, evaluateExamItemId };
        },
        setTaskStartTime(state, { payload: taskStartTime }) {
            return { ...state, taskStartTime };
        },
        setTaskEndTime(state, { payload: taskEndTime }) {
            return { ...state, taskEndTime };
        },
        setEvaluateObjectModel(state, { payload: evaluateObjectModel }) {
            return { ...state, evaluateObjectModel };
        },
        setEvaluatePlanSnapshotsModel(state, { payload: evaluatePlanSnapshotsModel }) {
            return { ...state, evaluatePlanSnapshotsModel };
        },
        setQuestionSnapshotsModelList(state, { payload: questionSnapshotsModelList }) {
            return { ...state, questionSnapshotsModelList };
        },
        setInitExamItemAnswerDetailMsgList(state, { payload: initExamItemAnswerDetailMsgList }) {
            return { ...state, initExamItemAnswerDetailMsgList };
        },
        setShowBtn(state, { payload: showBtn }) {
            return { ...state, showBtn };
        },
        setEvaluateTaskType(state, { payload: evaluateTaskType }) {
            return { ...state, evaluateTaskType };
        },
        setExpertCheckCourseInfo(state, { payload: expertCheckCourseInfo }) {
            return { ...state, expertCheckCourseInfo };
        },
    },
    effects: {
        /**
         * @desc 评价数据请求
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getDetailData({ payload }, { call, put, select }) {
            const result = yield call(getEvaluateExamItemDetail, payload);
            if (result.status === 'success') {
                const detailData = result.result;
                const {
                    evaluateExamItemId,
                    taskStartTime,
                    taskEndTime,
                    evaluateObjectModel,
                    evaluatePlanSnapshotsModel,
                    questionSnapshotsModelList,
                    evaluateExamItemAnswerDetailModelList,
                    evaluateTaskType,
                    expertCheckCourseInfo,
                } = detailData;
                if (evaluateExamItemAnswerDetailModelList.length === 0) {
                    yield put({
                        type: 'setShowBtn',
                        payload: true,
                    });
                } else {
                    yield put({
                        type: 'setShowBtn',
                        payload: false,
                    });
                }
                yield put({
                    type: 'setEvaluateExamItemId',
                    payload: evaluateExamItemId,
                });
                yield put({
                    type: 'setTaskStartTime',
                    payload: taskStartTime,
                });
                yield put({
                    type: 'setTaskEndTime',
                    payload: taskEndTime,
                });

                yield put({
                    type: 'setEvaluateObjectModel',
                    payload: evaluateObjectModel,
                });
                yield put({
                    type: 'setEvaluatePlanSnapshotsModel',
                    payload: evaluatePlanSnapshotsModel,
                });
                yield put({
                    type: 'setQuestionSnapshotsModelList',
                    payload: questionSnapshotsModelList,
                });
                yield put({
                    type: 'setExpertCheckCourseInfo',
                    payload: expertCheckCourseInfo,
                });
                if (evaluateExamItemAnswerDetailModelList.length === 0) {
                    const initExamItemAnswerDetailMsgList = questionSnapshotsModelList.map(item => {
                        return {
                            evaluateQuestionSnapshotsId: item.evaluateQuestionSnapshotsId, // 内容
                            planSnapshotsDetailId: '', // 方案
                        };
                    });
                    yield put({
                        type: 'setInitExamItemAnswerDetailMsgList',
                        payload: initExamItemAnswerDetailMsgList,
                    });
                } else {
                    yield put({
                        type: 'setInitExamItemAnswerDetailMsgList',
                        payload: evaluateExamItemAnswerDetailModelList,
                    });
                }
                yield put({
                    type: 'setEvaluateTaskType',
                    payload: evaluateTaskType,
                });

            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 提交评价
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* submitFetch({ payload, callback }, { call, put, select }) {
            const result = yield call(answerEvaluateExamItem, payload);
            if (result.status === 'success') {
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                const match = pathToRegexp('/my-tasks/:id').exec(pathname);
                if (match) {
                    dispatch({
                        type: 'getDetailData',
                        payload: {
                            evaluateExamItemId: match[1],
                        },
                    });
                }
            });
        },
    },
};
